Conditions | 1 |
Paths | 1 |
Total Lines | 121 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
16 | function (state, data, visibility, util, format, reactionService) { |
||
17 | let ct = this; |
||
18 | ct.state = state; |
||
19 | ct.data = data; |
||
20 | ct.util = util; |
||
21 | ct.format = format; |
||
22 | ct.adjustAmount = [1, 10, 25, 100]; |
||
23 | |||
24 | function update(player) { |
||
25 | for(let slot of player.element_slots){ |
||
26 | if(!slot){ |
||
27 | continue; |
||
28 | } |
||
29 | for (let reaction of slot.reactions) { |
||
30 | if (!reaction.active) { |
||
31 | continue; |
||
32 | } |
||
33 | reactionService.react(numberToReact(player, reaction.reaction), reaction.reaction, player); |
||
34 | } |
||
35 | } |
||
36 | } |
||
37 | |||
38 | function numberToReact(player, reaction) { |
||
39 | let power = ct.reactionPower(player); |
||
40 | let number = power; |
||
41 | for(let resource in reaction.reactants){ |
||
42 | number = Math.min(number, player.resources[resource].number); |
||
43 | } |
||
44 | return number; |
||
45 | } |
||
46 | |||
47 | /* Calculates the reaction power based on the reaction upgrades */ |
||
48 | ct.reactionPower = function(player) { |
||
49 | let level = player.global_upgrades_current.reaction_bandwidth; |
||
50 | let upgrade = data.global_upgrades.reaction_bandwidth; |
||
51 | let basePower = upgrade.power; |
||
52 | let polynomial = upgrade.power_poly; |
||
53 | return basePower * Math.floor(Math.pow(level, polynomial)); |
||
54 | }; |
||
55 | |||
56 | /* Calculates the number of reaction slots based on the reaction upgrades */ |
||
57 | ct.reactionSlots = function (player) { |
||
58 | let level = player.global_upgrades.reaction_slots; |
||
59 | let upgrade = data.global_upgrades.reaction_slots; |
||
60 | let basePower = upgrade.power; |
||
61 | let multiplier = upgrade.power_mult; |
||
62 | return basePower * Math.floor(multiplier * level); |
||
63 | }; |
||
64 | |||
65 | ct.reactionSize = function (player) { |
||
66 | let size = 0; |
||
67 | for(let slot of player.element_slots){ |
||
68 | if(!slot){ |
||
69 | continue; |
||
70 | } |
||
71 | size += slot.reactions.length; |
||
72 | } |
||
73 | return size; |
||
74 | }; |
||
75 | |||
76 | /* Adds a new reaction to the player list */ |
||
77 | ct.addReaction = function (player, slot, key) { |
||
78 | if(ct.reactionSize(player) >= ct.reactionSlots(player)){ |
||
79 | return; |
||
80 | } |
||
81 | let reaction = data.reactions[key]; |
||
82 | slot.reactions.push({ |
||
83 | active: false, |
||
84 | reaction: angular.copy(reaction) |
||
85 | }); |
||
86 | }; |
||
87 | |||
88 | ct.removeReaction = function (slot, item) { |
||
89 | for(let i = 0; i < slot.reactions.length; i++){ |
||
90 | if(slot.reactions[i] === item){ |
||
91 | slot.reactions.splice(i, 1); |
||
92 | } |
||
93 | } |
||
94 | }; |
||
95 | |||
96 | ct.visibleReactions = function(slot) { |
||
97 | return slot.reactions; |
||
98 | }; |
||
99 | |||
100 | ct.availableReactions = function(slot) { |
||
101 | return visibility.visible(data.reactions, isReactionAvailable, slot.element); |
||
102 | }; |
||
103 | |||
104 | function isReactionAvailable(entry, currentElement) { |
||
105 | let available = true; |
||
106 | let reaction = data.reactions[entry]; |
||
107 | for(let resource in reaction.reactant){ |
||
108 | available = available && state.player.resources[resource].unlocked; |
||
109 | } |
||
110 | // Workaround to reuse the visibility function. It expects an object with the |
||
111 | // reaction inside |
||
112 | let reactionObject = {reaction:reaction}; |
||
113 | return available && isReactionVisible(reactionObject, currentElement); |
||
114 | } |
||
115 | |||
116 | function isReactionVisible(entry, currentElement) { |
||
117 | let reaction = entry.reaction; |
||
118 | if(reaction.elements.length === 0){ |
||
119 | return true; |
||
120 | } |
||
121 | for(let element of reaction.elements){ |
||
122 | if(element === currentElement){ |
||
123 | return true; |
||
124 | } |
||
125 | } |
||
126 | return false; |
||
127 | } |
||
128 | |||
129 | ct.adjustLevel = function(player, upgrade, amount){ |
||
130 | player.global_upgrades_current[upgrade] += amount; |
||
131 | // We cap it between 1 and the current max level |
||
132 | player.global_upgrades_current[upgrade] = Math.max(1, Math.min(player.global_upgrades_current[upgrade], player.global_upgrades[upgrade])); |
||
133 | }; |
||
134 | |||
135 | state.registerUpdate('reactions', update); |
||
136 | }]); |
||
137 |